package com.gemme.gemme.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.gemme.gemme.R;
import com.gemme.gemme.gwc.GwcBean2;
import com.gemme.gemme.gwc.GwcNewFragment;
import com.gemme.gemme.home.BrandActivity;
import com.gemme.gemme.home.BrandListActivity;
import com.gemme.gemme.wode.ProductNewActivity;

public class GwcItemView extends RelativeLayout {
    LinearLayout gwc_content;
    boolean isAllSelect = false;
    GwcItemTopView gwc_top;
    private Context context;
    private OnChangeLIstener onChangeLIstener;
    //每个商品的监听
    OnChangeLIstener singleProOnChangeLIstener = new OnChangeLIstener() {
        @Override
        public void onChangeLIstener() {
            //判断是否全选
            isAllSelect();
            gwc_top.getCheckbox().setChecked(isAllSelect);
            // todo  全选变化处1
            if (onChangeLIstener != null) {
                onChangeLIstener.onChangeLIstener();
            }
        }
    };
    private GwcBean2.DataBean.RecordsBean recordsBean;
    private GwcNewFragment gwcNewFragment;

    public GwcItemView(Context context) {
        this(context, null);
    }

    public GwcItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GwcItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
//        initData();
    }

    private void initData() {
       /* gwc_content.addView(new GwcSingleItemView(getContext()).setOnChangeLIstener(singleProOnChangeLIstener));
        gwc_content.addView(new GwcSingleItemView(getContext()).setOnChangeLIstener(singleProOnChangeLIstener));*/
    }

    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.gwc_item, this);
        gwc_top = findViewById(R.id.gwc_top);
        gwc_content = findViewById(R.id.gwc_content);
        setTopSelect();

    }

    //顶部--就是控制商店全选
    private void setTopSelect() {
        gwc_top.getCheckbox().setOnChangeLIstener(new OnChangeLIstener() {
            @Override
            public void onChangeLIstener() {
                isAllSelect = gwc_top.getCheckbox().isChecked();
                gwc_top.getCheckbox().setChecked(isAllSelect);

                controlAllSelect();

                // todo  全选变化处2
                if (onChangeLIstener != null) {
                    onChangeLIstener.onChangeLIstener();
                }
            }
        });
    }

    public void controlAllSelect() {
        try {
            for (int i = 0; i < gwc_content.getChildCount(); i++) {
                View childAt = gwc_content.getChildAt(i);
                GwcSingleItemView itemview = (GwcSingleItemView) childAt;
                itemview.getCheckbox().setChecked(isAllSelect);
                System.out.println("");
            }

        } catch (Exception e) {

        }
    }

    private void isAllSelect() {

        try {
            for (int i = 0; i < gwc_content.getChildCount(); i++) {
                View childAt = gwc_content.getChildAt(i);
                GwcSingleItemView itemview = (GwcSingleItemView) childAt;
                boolean checked = itemview.getCheckbox().isChecked();
                if (!checked) {
                    isAllSelect = false;
                    return;
                }
            }

            isAllSelect = true;
        } catch (Exception e) {

        }
    }


    public GwcItemView setOnItemAllLIstener(OnChangeLIstener onChangeLIstener) {

        this.onChangeLIstener = onChangeLIstener;
        return this;
    }

    public boolean getIsAllSelect() {
        return isAllSelect;
    }

    public void setIsAllSelect(boolean istrue) {
        isAllSelect = istrue;
        controlAllSelect();
        gwc_top.getCheckbox().setChecked(istrue);
    }

    public GwcBean2.DataBean.RecordsBean getData() {
        return recordsBean;
    }

    /* gwc_content.addView(new GwcSingleItemView(getContext()).setOnChangeLIstener(singleProOnChangeLIstener));
            gwc_content.addView(new GwcSingleItemView(getContext()).setOnChangeLIstener(singleProOnChangeLIstener));*/
    public void setData(GwcBean2.DataBean.RecordsBean recordsBean) {
        this.recordsBean = recordsBean;
        try {
            gwc_top.setShopname(recordsBean.getBrandName());
            GwcSingleItemView gwcSingleItemView = new GwcSingleItemView(getContext()).setOnChangeLIstener(singleProOnChangeLIstener);
            gwcSingleItemView.setFragment(gwcNewFragment);
            gwcSingleItemView.setData(recordsBean);
            gwc_content.addView(gwcSingleItemView);

            /*List<GwcBean.DataBean.RecordsBean.ListBean> list = recordsBean.getList();
            for (int i = 0; i < list.size(); i++) {
                GwcSingleItemView gwcSingleItemView = new GwcSingleItemView(getContext()).setOnChangeLIstener(singleProOnChangeLIstener);
                gwcSingleItemView.setData(list.get(i));
                gwc_content.addView(gwcSingleItemView);
            }*/

            gwc_top.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    //开始
                    Intent intent = new Intent(getContext(), BrandListActivity.class);
                    intent.putExtra("brandName",recordsBean.getBrandName());
                    intent.putExtra("brandLogo",recordsBean.getBrandLogo());
                    context.startActivity(intent);
                }
            });
        } catch (Exception e) {

        }
    }

    public void setSingleDeleteIsVisible(boolean isEdit) {
        try {
            for (int i = 0; i < gwc_content.getChildCount(); i++) {
                View childAt = gwc_content.getChildAt(i);
                GwcSingleItemView itemview = (GwcSingleItemView) childAt;
                itemview.setSingleDeleteIsVisible(isEdit);
            }

        } catch (Exception e) {

        }
    }


    public void setFragment(GwcNewFragment gwcNewFragment) {

        this.gwcNewFragment = gwcNewFragment;
    }
}
